<template>
  <div class="toc-sidebar">
    <div class="toc-header">
      <h3 @click="test">文章目录</h3>
      <Button icon="fas fa-eye-slash" tip="隐藏菜单" @click-it="emit('hideMenu')"></Button>
    </div>
    <ul v-if="props.toc !== undefined && props.toc.length" class="toc-list">
      <li
        v-for="item in props.toc"
        :key="item.id"
        :class="{
          active: props.activeId === item.text,
          expanded: props.activeParentId === item.text,
        }">
        <a :href="`#${item.text}`" @click.prevent="scrollTo(item.text)">{{ item.text }}</a>
        <ul v-if="item.children.length > 0">
          <li
            v-for="child in item.children"
            :key="child.id"
            :class="{ active: props.activeId === child.text }">
            <a :href="`#${child.text}`" @click.prevent="scrollTo(child.text)">{{ child.text }}</a>
          </li>
        </ul>
      </li>
    </ul>
    <p v-else>暂无内容（只显示二/三级目录）</p>
  </div>
</template>

<script setup>
import Button from "@/components/Button.vue"

const props = defineProps({
  toc: Object,
  activeId: String,
  activeParentId: String,
})

const emit = defineEmits(["hideMenu"])

// 滚动到指定标题
const scrollTo = (id) => {
  const element = document.getElementById(id)
  if (element) {
    element.style.scrollMarginTop = "70px"
    element.scrollIntoView({ behavior: "smooth" })
  }
}
</script>

<style scoped>
.toc-sidebar {
  position: sticky;
  top: 60px;
  height: fit-content;
  max-height: calc(100vh - 80px);
  overflow-y: auto;
  background: var(--card-bg);
  backdrop-filter: blur(10px);
  border-radius: var(--border-radius);
  padding: 20px;
  transition: var(--transition);
  box-shadow: var(--shadow);
  border: 1px solid var(--glass-border);
}

.toc-sidebar:hover {
  transform: translateY(-5px);
  box-shadow: var(--shadow);
  border-color: rgba(var(--primary-rgb), 0.5);
}

.toc-sidebar::-webkit-scrollbar {
  width: 6px;
}

.toc-sidebar::-webkit-scrollbar-track {
  background: rgba(var(--bg-darker-rgb), 0.1);
  border-radius: 15px;
}

.toc-sidebar::-webkit-scrollbar-thumb {
  background: var(--primary);
  border-radius: 5px;
}

.toc-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  color: var(--text);
  padding-bottom: 15px;
  border-bottom: 1px solid var(--glass-border);
}

.header-buttons {
  display: flex;
  gap: 10px;
}

.toc-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.toc-list > li {
  margin-bottom: 4px;
}

.toc-list > li > a {
  display: block;
  color: var(--text);
  text-decoration: none;
  transition: var(--transition);
}

.toc-list ul {
  list-style: none;
  margin-bottom: 4px;
  display: none;
  transition: max-height 0.4s ease;
}

.toc-list li.expanded ul {
  display: inline-block;
  max-height: 500px;
}

.toc-list ul li a {
  display: block;
  padding: 6px 20px;
  border-radius: 6px;
  color: var(--text);
  text-decoration: none;
  font-size: 0.9rem;
  transition: all 0.3s ease;
}

.toc-list > li > a::before,
.toc-list ul li a::before {
  content: "";
  width: 5px;
  height: 10px;
  background: var(--primary);
  display: inline-block;
  vertical-align: middle;
  margin-right: 11px;
  transition: 0.3s;
}

.toc-list > li > a:hover::before,
.toc-list ul li a:hover::before {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  opacity: 1;
}

.toc-list > li > a:hover {
  color: var(--primary);
}

.toc-list ul li.active a,
.toc-list > li.active > a,
.toc-list > li.expanded > a {
  color: var(--primary);
}

@media (max-width: 768px) {
  .toc-sidebar {
    position: relative;
    top: 0;
    max-height: none;
    margin-bottom: 30px;
  }
}
</style>
